<template>
  <TableView class="table">
    <template #form>
      <NSpace justify="space-between">
        <NSpace align="center">
          库房名称:
          <NInput style="width: 200rem" placeholder="请输入库房名称" v-model:value="paginationReactive.warehouseName"
            clearable />
          <NButton type="primary" style="width: 78rem" ghost
            @click="() => ((paginationReactive.page = 1), getDetopManagementList())">
            查询
          </NButton>
        </NSpace>
        <NButton type="primary" style="width: 115rem"
          @click="$router.push({ path: '/OA/AssetsManagement/DetopManagement/DetopAdd' })">
          新建库房
        </NButton>
      </NSpace>
    </template>

    <template #table="props">
      <NDataTable v-bind="props" :bordered="false" :columns="columns" :data="tableData" :pagination="false"
        :loading="tableLoading" children-key="childList" :row-key="(e) => e.id" />
    </template>

    <template #pagination>
      <NPagination :page-sizes="paginationReactive.pageSizes" :item-count="paginationReactive.itemCount"
        :page="paginationReactive.page" :page-size="paginationReactive.pageSize"
        @update:page="paginationReactive.onChange" @update:page-size="paginationReactive.onUpdatePageSize"
        show-size-picker show-quick-jumper :suffix="() => '页'">
        <template #prefix>
          <span style="font-size: 14px;"> 共{{ paginationReactive.itemCount }}条</span>
        </template>
      </NPagination>
    </template>
  </TableView>

  <NModal :auto-focus="false" v-model:show="modalVisible" @close="modalVisible = false">
    <VDialogContent @close="modalVisible = false" @submit="detopDelete" title="删除提示">
      <div style="width: 300rem; height: 100rem; text-align: center; line-height: 100rem">
        您确定删除{{ detopInfo.warehouseName }}吗？
      </div>
    </VDialogContent>
  </NModal>
</template>
<script setup lang="ts">
import { NSpace, NButton, NDataTable, NPagination, useMessage, NModal, NInput } from 'naive-ui'
import { h, reactive, ref, watch, onDeactivated } from 'vue'
import { sendRequest } from '@/utils/api'
import { useRoute } from 'vue-router'
import type { DataTableColumns } from 'naive-ui'
import TableView from '@/components/TableView.vue'
import VDialogContent from '@/views/OA/Common/DialogContent.vue'
import router from '@/router'
import { useViewerStore } from '@/views/OA/Common/store'

const useStore = useViewerStore()
const message = useMessage()
const route = useRoute()
const modalVisible = ref(false)

const columns: DataTableColumns = [
  {
    title: '库房名称',
    key: 'warehouseName',
    width: 60,
    align: 'left'
  },
  {
    title: '上级库房名称',
    key: 'parentName',
    width: 60,
    align: useStore.isCenter
  },
  {
    title: '库房备注',
    key: 'remark',
    width: 60,
    align: useStore.isCenter,
    ellipsis: {
      tooltip: {
        contentStyle: {
          maxWidth: '400rem'
        }
      }
    }
  },
  {
    title: '创建人',
    key: 'createBy',
    width: 60,
    align: useStore.isCenter
  },
  {
    title: '创建时间',
    key: 'createTime',
    width: 60,
    align: useStore.isCenter
  },
  {
    title: '操作',
    key: 'action',
    align: useStore.isCenter,
    width: 60,
    fixed: 'right',
    render: (row: any) => [
      h(
        NButton,
        {
          type: 'primary',
          color: useStore.color.delete,
          quaternary: true,
          onClick: async () => {
            detopInfo.value = row
            modalVisible.value = true
          }
        },
        {
          default: () => '删除'
        }
      ),
      h(
        NButton,
        {
          type: 'primary',
          quaternary: true,
          onClick: async () =>
            router.push({ path: '/OA/AssetsManagement/DetopManagement/DetopUpdate', query: { id: row.id } })
        },
        {
          default: () => '编辑'
        }
      )
    ]
  }
]
// 上一页下一页
const paginationReactive = reactive({
  page: 1,
  pageSize: 10,
  pageSizes: [10, 20, 50, 100, 500],
  itemCount: 0,
  warehouseName: null, //类目名称
  onChange: (page: number) => {
    paginationReactive.page = page
    getDetopManagementList()
  },
  onUpdatePageSize: (pageSize: number) => {
    paginationReactive.pageSize = pageSize
    paginationReactive.page = 1
    getDetopManagementList()
  }
})
// 删除
const detopInfo = ref()
const detopDelete = async () => {
  modalVisible.value = false
  tableLoading.value = true
  const [error, res] = await sendRequest({
    module: 'assetsManagement',
    api: 'detopManagementDelete',
    override: {
      data: [detopInfo.value.id]
    }
  })
  if (error) return message.error('删除失败'), (tableLoading.value = false)
  if (res.code !== 0) return message.error('删除失败'), (tableLoading.value = false)
  getDetopManagementList()
  message.success('删除成功')
}
// 获取列表
const tableData: any = ref([])
const tableLoading = ref(false)
const getDetopManagementList = async () => {
  tableLoading.value = true
  const [error, res] = await sendRequest({
    module: 'assetsManagement',
    api: 'detopManagementList',
    override: {
      params: {
        page: paginationReactive.page,
        limit: paginationReactive.pageSize,
        warehouseName: paginationReactive.warehouseName
      }
    }
  })
  tableLoading.value = false
  if (error) return message.error(error.message)
  if (res.code !== 0) return message.error(res.msg)
  setTimeout(() => {
    tableData.value = res.data.list || []
    paginationReactive.itemCount = res.data.totalCount
  }, 0)
}

const keyUpDown = (e: any) => e.key == 'Enter' && getDetopManagementList()
onDeactivated(() => window.removeEventListener('keyup', keyUpDown, false))

watch(
  () => route,
  async () => {
    if (route.path == '/OA/AssetsManegement/DetopManagement/DetopManagement') {
      getDetopManagementList()
      window.addEventListener('keyup', keyUpDown, false)  // 回车
    }
  },
  { deep: true, immediate: true }
)

</script>
<style scoped lang="scss">
.table {
  padding: 29rem 30rem 42rem;
}
</style>
    